/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.commons.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.shop.cereshop.commons.constant.WalletConstant;

import java.util.regex.Pattern;

/**
 * 支付密码工具类
 * @author
 */
public class PayPasswordUtils {

    /**
     * 验证支付密码格式
     * @param payPassword 支付密码
     * @return 是否有效
     */
    public static boolean isValidPayPassword(String payPassword) {
        if (StrUtil.isBlank(payPassword)) {
            return false;
        }
        // 必须是6位数字
        return Pattern.matches("^\\d{6}$", payPassword);
    }

    /**
     * 加密支付密码
     * @param payPassword 原始支付密码
     * @return 加密后的密码
     */
    public static String encryptPayPassword(String payPassword) {
        if (StrUtil.isBlank(payPassword)) {
            return null;
        }
        return BCrypt.hashpw(payPassword, BCrypt.gensalt());
    }

    /**
     * 验证支付密码
     * @param payPassword 原始支付密码
     * @param encryptedPassword 加密后的密码
     * @return 是否匹配
     */
    public static boolean verifyPayPassword(String payPassword, String encryptedPassword) {
        if (StrUtil.isBlank(payPassword) || StrUtil.isBlank(encryptedPassword)) {
            return false;
        }
        return BCrypt.checkpw(payPassword, encryptedPassword);
    }

    /**
     * 生成支付密码盐值
     * @return 盐值
     */
    public static String generateSalt() {
        return BCrypt.gensalt();
    }

    /**
     * 检查支付密码是否被锁定
     * @param errorCount 错误次数
     * @param lockTime 锁定时间
     * @return 是否锁定
     */
    public static boolean isPayPasswordLocked(Integer errorCount, String lockTime) {
        if (errorCount == null || errorCount < WalletConstant.PayPasswordConfig.MAX_ERROR_COUNT) {
            return false;
        }
        
        if (StrUtil.isBlank(lockTime)) {
            return false;
        }
        
        // 检查是否还在锁定时间内
        long lockTimeMillis = TimeUtils.parseDate(lockTime).getTime();
        long currentTimeMillis = System.currentTimeMillis();
        long lockDurationMillis = WalletConstant.PayPasswordConfig.LOCK_TIME_MINUTES * 60 * 1000L;
        
        return (currentTimeMillis - lockTimeMillis) < lockDurationMillis;
    }

    /**
     * 计算剩余锁定时间(分钟)
     * @param lockTime 锁定时间
     * @return 剩余锁定时间(分钟)，0表示已解锁
     */
    public static long getRemainingLockTime(String lockTime) {
        if (StrUtil.isBlank(lockTime)) {
            return 0;
        }
        
        long lockTimeMillis = TimeUtils.parseDate(lockTime).getTime();
        long currentTimeMillis = System.currentTimeMillis();
        long lockDurationMillis = WalletConstant.PayPasswordConfig.LOCK_TIME_MINUTES * 60 * 1000L;
        long elapsedTime = currentTimeMillis - lockTimeMillis;
        
        if (elapsedTime >= lockDurationMillis) {
            return 0;
        }
        
        return (lockDurationMillis - elapsedTime) / (60 * 1000);
    }

    /**
     * 生成随机6位数字支付密码
     * @return 随机支付密码
     */
    public static String generateRandomPayPassword() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < WalletConstant.PayPasswordConfig.PASSWORD_LENGTH; i++) {
            sb.append((int) (Math.random() * 10));
        }
        return sb.toString();
    }
}
